package com.xinghen.security.core.validate.code.image;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinghen.security.core.validate.code.ValidateCodeGenerator;
import com.xinghen.security.core.validate.code.ValidateCodeRepository;
import com.xinghen.security.core.validate.code.impl.AbstractValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import java.util.Map;

/**
 * @author xinghen
 * @ClassName: ImageValidateCodeProcessor
 * @Package: com.xinghen.security.core.validate.code.image
 * @Description 图片验证码处理器
 * @create 2018-04-18 15:35
 */
@Component
public class ImageValidateCodeProcessor extends AbstractValidateCodeProcessor<ImageValidateCode> {

    @Autowired
    private ObjectMapper objectMapper;

    public ImageValidateCodeProcessor(Map<String, ValidateCodeGenerator> validateCodeGenerators, ValidateCodeRepository validateCodeRepository) {
        super(validateCodeGenerators, validateCodeRepository);
    }

    @Override
    protected void send(ServletWebRequest request, ImageValidateCode validateCode) throws Exception {
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(validateCode.getImage(), "JPEG", request.getResponse().getOutputStream());
//        SecurityResult result = SecurityResult.ok(baos.toByteArray());
//        HttpServletResponse response = request.getResponse();
//        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
//        response.getWriter().write(objectMapper.writeValueAsString(result));
    }
}
